package cn.qitom.auth.granter;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.qitom.core.tool.api.R;
import cn.qitom.service.user.entity.UserBase;
import cn.qitom.service.user.feign.IUserClient;

import java.util.HashMap;

public class PhoneGranter implements ILegoGranter{
    public static String ROUTE = "code";
    private GranterProp granterProp;
    private IUserClient userClient;



    public PhoneGranter(GranterProp granterProp,IUserClient userClient) {
        this.granterProp = granterProp;
        this. userClient = userClient;
    }

    @Override
    public Object login() {


        R<UserBase> userBaseR= null;
        userBaseR = userClient.userLoginInfoByAccount(granterProp.getPhone(), granterProp.getPassword());

        if(userBaseR.ok()) {
            StpUtil.setLoginId(userBaseR.getData().getId());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            R<UserBase> finalUserBaseR = userBaseR;

            HashMap map = new HashMap() {{
                put("token", tokenInfo);
                put("user", finalUserBaseR.getData());
            }};

            return map;
        }

        return userBaseR;

    }
}
